package com.example.community.update;

import com.example.community.dao.UsersDao;
import com.example.community.model.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(urlPatterns = "/userUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        UsersDao usersDao = new UsersDao();
        System.out.println("aaaaaaaaaaaaa"+uid);
        Users user = usersDao.fandById(Integer.parseInt(uid));
        System.out.println(user);
        req.setAttribute("user",user);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String format = sdf.format(user.getUsBirthday());
            System.out.println("format"+format);
            req.setAttribute("format",format);
        }catch (Exception e){
            System.out.println("error");
            req.setAttribute("format",null);
        }

        req.getRequestDispatcher("/WEB-INF/userManager/userUpdate.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        String loginname = req.getParameter("loginname");
        String password = req.getParameter("password");
        String username = req.getParameter("username");
        String sex = req.getParameter("sex");
        String birthday = req.getParameter("birthday");
        String tel = req.getParameter("tel");
//        System.out.println("获取的值是："+uid);
//        System.out.println("获取的值是："+loginname);
//        System.out.println("获取的值是："+password);
//        System.out.println("获取的值是："+username);
//        System.out.println("获取的值是："+sex);
//        System.out.println("获取的值是："+birthday);
        Users user = new Users();
        user.setUsNo(Integer.parseInt(uid));
        user.setUsAccount(loginname);
        user.setUsPwd(password);
        user.setUsName(username);
        user.setUsSex(sex);
        user.setUsTel(tel);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = sdf.parse(birthday);
            user.setUsBirthday(parse);
        } catch (ParseException e) {
            e.printStackTrace();
//            resp.getWriter().write("false");
        }finally {
            UsersDao usersDao = new UsersDao();
            Integer insert = usersDao.update(user);
            if (insert == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }

    }
}
